package com.penghaisoft.microwms.common.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.MesProductTypePager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;

@Slf4j
@Service("mesInterfaceService")
public class MesInterfaceServiceImpl implements IMesInterfaceService {

	public Map<String, Object> publicWS(String url, MediaType mediaType,
			String paramJson) {
		Map<String, Object> result = new HashMap<String, Object>();
		String resResult = "";

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		try {
			Response response = target.request().post(
					Entity.entity(paramJson, mediaType));
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : "
						+ response.getStatus());
			}
			resResult = response.readEntity(String.class);
			result.put("success", true);
			response.close();

		} catch (Exception e) {
			result.put("success", false);
			log.error("MES服务器异常，请联系管理员！", e);
		} finally {
			result.put("result", resResult);
			client.close();
		}
		return result;
	}

	public JSONObject publicWSJson(String url, MediaType mediaType,
			String paramJson) {
		JSONObject jsonObject = new JSONObject();
		String result = "";
		Client client = ClientBuilder.newClient();

		WebTarget target = client.target(url);
		try {
			Response response = target.request().post(
					Entity.entity(paramJson, mediaType));
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : "
						+ response.getStatus());
			}
			result = response.readEntity(String.class);
			jsonObject = JSONObject.parseObject(result);
			response.close();
		} catch (Exception e) {
			jsonObject.put("code", "0");
			jsonObject.put("message", "MES服务器异常，请联系管理员！");
		} finally {
			client.close();
		}

		return jsonObject;
	}
	
	public HandResp publicWSResp(String url, MediaType mediaType, String paramJson){
		HandResp resp = new HandResp();
		JSONObject jsonObject = new JSONObject();
		String result = "";
		Client client = ClientBuilder.newClient();
		
		WebTarget target = client.target(url);
		try {
			Response response = target.request().post(
					Entity.entity(paramJson, mediaType));
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : "
						+ response.getStatus());
			}
			result = response.readEntity(String.class);
			jsonObject = JSONObject.parseObject(result);
			resp.setCode(jsonObject.getString("code"));
			resp.setMsg(jsonObject.getString("message"));
			response.close();
		} catch (Exception e) {
			resp.setCode("0");
			resp.setMsg("MES服务器异常，请联系管理员！");
		} finally {
			client.close();
		}

		return resp;
	}
	
	public TransResult publicTransResult(String url, MediaType mediaType, String paramJson){
		TransResult resp = new TransResult();
		JSONObject jsonObject = new JSONObject();
		String result = "";
		Client client = ClientBuilder.newClient();
		
		WebTarget target = client.target(url);
		try {
			Response response = target.request().post(
					Entity.entity(paramJson, mediaType));
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : "
						+ response.getStatus());
			}
			result = response.readEntity(String.class);
			jsonObject = JSONObject.parseObject(result);
			resp.setCode(jsonObject.getString("code"));
			resp.setMsg(jsonObject.getString("message"));
			resp.setData(JSONObject.toJavaObject(JSONObject.parseObject(jsonObject.getString("data")), MesProductTypePager.class));
			response.close();
		} catch (Exception e) {
			resp.setCode("0");
			resp.setMsg("MES服务器异常，请联系管理员！");
		} finally {
			client.close();
		}

		return resp;
	}
}
